/*
 * Copyright (C) 2010 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package app.tellwmata;

import android.content.Context;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.EditTextPreference;
import android.preference.Preference;

import android.preference.Preference.OnPreferenceChangeListener;

import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import static app.tellwmata.Constants.FormFields.ADDRESS_1;
import static app.tellwmata.Constants.FormFields.ADDRESS_2;
import static app.tellwmata.Constants.FormFields.CITY;
import static app.tellwmata.Constants.FormFields.EMAIL;
import static app.tellwmata.Constants.FormFields.FIRST_NAME;
import static app.tellwmata.Constants.FormFields.LAST_NAME;
import static app.tellwmata.Constants.FormFields.STATE;
import static app.tellwmata.Constants.FormFields.TELEPHONE;
import static app.tellwmata.Constants.FormFields.ZIP_CODE;

public final class Profile extends PreferenceActivity {
    public static String getFirstName(final Context c) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return prefs.getString(FIRST_NAME, Constants.PROMPT);
    }

    public static String getLastName(final Context c) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return prefs.getString(LAST_NAME, Constants.PROMPT);
    }

    public static String getEmail(final Context c) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return prefs.getString(EMAIL, Constants.PROMPT);
    }

    SharedPreferences prefs;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        setPreferenceScreen(createPreferenceHierarchy());
    }

    private PreferenceScreen createPreferenceHierarchy() {
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

        addTextPref(root, FIRST_NAME, R.string.FirstName);
        addTextPref(root, LAST_NAME, R.string.LastName);
        addTextPref(root, EMAIL, R.string.EmailAddress);
        addTextPref(root, ADDRESS_1, R.string.Address1);
        addTextPref(root, ADDRESS_2, R.string.Address2);
        addTextPref(root, CITY, R.string.City);
        addTextPref(root, STATE, R.string.State);
        addTextPref(root, ZIP_CODE, R.string.ZipCode);
        addTextPref(root, TELEPHONE, R.string.Telephone);

        return root;
    }

    private void addTextPref(PreferenceScreen root, String fieldName, final int titleId) {
        EditTextPreference txtPref = new EditTextPreference(this);
        txtPref.setKey(fieldName);
        txtPref.setTitle(titleId);
        txtPref.setSummary(prefs.getString(fieldName, ""));

        txtPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    preference.setSummary(newValue.toString());

                    return true;
                }
            });

        if (prefs.contains(fieldName)) {
            txtPref.setText(prefs.getString(fieldName, "<<NULL>>"));
        }

        root.addPreference(txtPref);
    }
}
